﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class FlowRunDataDao
    {
        public int Insert(SqlConnection conn, FlowRunDataData data)
        {
            string commandText = "INSERT INTO [dbo].[Flow_Run_Data] ( [RunId], [ItemId], [ItemData] ) VALUES ( @RunId, @ItemId, @ItemData ) SELECT SCOPE_IDENTITY() AS RetValue";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = data.RunId;
            commandParameters[1] = new SqlParameter("@ItemId", SqlDbType.Int);
            commandParameters[1].Value = data.ItemId;
            commandParameters[2] = new SqlParameter("@ItemData", SqlDbType.NText);
            if (data.ItemDataIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.ItemData;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectByRunId(SqlConnection conn, int runId)
        {
            string commandText = "SELECT [RunId], [ItemId], [ItemData] FROM [dbo].[Flow_Run_Data] WHERE [RunId] = @RunId ORDER BY [ItemId] ASC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RunId", SqlDbType.Int) };
            commandParameters[0].Value = runId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run_Data";
            return set.Tables[0];
        }

        public void Update(SqlConnection conn, FlowRunDataData data)
        {
            string commandText = "IF EXISTS(SELECT [RunId] FROM [dbo].[Flow_Run_Data] WHERE [RunId] = @RunId AND [ItemId] = @ItemId ) UPDATE [dbo].[Flow_Run_Data] SET [ItemData] = @ItemData WHERE [RunId] = @RunId AND [ItemId] = @ItemId ELSE INSERT [dbo].[Flow_Run_Data] ( [RunId], [ItemId], [ItemData] ) VALUES ( @RunId, @ItemId, @ItemData )";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = data.RunId;
            commandParameters[1] = new SqlParameter("@ItemId", SqlDbType.Int);
            commandParameters[1].Value = data.ItemId;
            commandParameters[2] = new SqlParameter("@ItemData", SqlDbType.NText);
            if (data.ItemDataIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.ItemData;
            }
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }
    }
}

